package com.ruoyi.test;


import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class NetworkScanner {

    public static void main(String[] args) {
        try {
            scanSubnet("192.168.1.1");
            // 获取所有网络接口
//            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
//            while (networkInterfaces.hasMoreElements()) {
//                NetworkInterface networkInterface = networkInterfaces.nextElement();
//
//                // 检查是否为回环地址或虚拟地址
//                if (networkInterface.isUp() && !networkInterface.isLoopback() && !networkInterface.isVirtual()) {
//                    Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
//                    while (inetAddresses.hasMoreElements()) {
//                        InetAddress inetAddress = inetAddresses.nextElement();
//                        if (inetAddress instanceof Inet4Address) {
//                            System.out.println("Interface: " + networkInterface.getName());
//                            System.out.println("IP Address: " + inetAddress.getHostAddress());
//
//                            // 获取子网掩码
//                            int prefixLength = networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength();
//
//                            // 计算子网范围
//                            String subnet = calculateSubnet(inetAddress.getHostAddress(), prefixLength);
//                            if(!subnet.startsWith("192.168")) continue;
//                            System.out.println("Subnet: " + subnet);
//
//                            // 扫描子网
//                            scanSubnet(subnet);
//                        }
//                    }
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String calculateSubnet(String ipAddress, int prefixLength) {
        String[] parts = ipAddress.split("\\.");
        long ip = (Long.parseLong(parts[0]) << 24) |
                (Long.parseLong(parts[1]) << 16) |
                (Long.parseLong(parts[2]) << 8) |
                (Long.parseLong(parts[3]));

        long mask = -1L << (32 - prefixLength);
        long network = ip & mask;

        String subnet = (network >> 24) + "." +
                ((network >> 16) & 0xFF) + "." +
                ((network >> 8) & 0xFF) + "." +
                (network & 0xFF);

        return subnet;
    }

    private static void scanSubnet(String subnet) {
        for (int i = 1; i <= 254; i++) {
            String ip = subnet + "." + i;
            try {
                InetAddress address = InetAddress.getByName(ip);
                if (address.isReachable(1000)) { // 超时时间1秒
                    System.out.println("Device found: " + ip);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

